package com.lanou3g.HW_0517.model;

import com.lanou3g.HW_0517.pojo.User;
import com.lanou3g.HW_0517.util.JDBCUtil;
import com.lanou3g.HW_0517.util.MyResultSet;
import com.lanou3g.HW_0517.util.MyStatement;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * If there are no bugs, it was created by Chen FengYao on 2018/4/27;
 * Otherwise, I don't know who created it either
 */

/*
专门与数据库进行连接
*/
public class Model {

    /**
     * 判断数据库中 是否有某一个用户名
     *
     * @param username 要判断的 用户名
     * @return true: 数据库中有该用户; false: 数据库中没有该用户
     */
    public boolean hasUsername(String username) {

        String sql = "SELECT COUNT(*) FROM HW_0517 WHERE username = ?";
        MyStatement statement = JDBCUtil.getPreStatement(sql);
        statement.setString(1, username);
        // 这行语句执行完成后,不会自动关闭资源
        MyResultSet rs = statement.executeQuery();

        rs.next(); //索引移动到第一行
        int count = rs.getInt(1);
        rs.close();
        JDBCUtil.closeResource(statement);
        return count > 0;


    }

    /**
     *
     * @param username 用户名
     * @param pwd      密码
     * @param nickname 昵称
     * @param hobby    爱好
     * @param gender   性别
     * @return true: 注册成功; false 注册失败
     */
    public boolean register(String username,
                            String pwd,
                            String nickname,
                            String hobby,
                            String gender) {
        String sql = "INSERT INTO HW_0517 (username , pwd , nickname , hobby , gender) " +
                "VALUES ( ? , ? , ? , ? , ?)";
        MyStatement statement = JDBCUtil.getPreStatement(sql);
        statement.setString(1, username);
        statement.setString(2, pwd);
        statement.setString(3, nickname);
        statement.setString(4, hobby);
        statement.setString(5, gender);
        // 不需要结果集的 执行,可以实现自动关闭资源
        int rows = statement.executeUpdate();
        return rows > 0;
    }


    /**
     * 登录
     *
     * @param username 用户名
     * @param pwd      密码
     * @return 登录成功: 返回装着用户信息的User对象
     * 登录失败: return null
     */
    public User login(String username, String pwd) {
        // 1. 查一下 是否有 这个用户
        // 条件是 用户名,和密码 都匹配上

        String checkLogin = "SELECT * FROM HW_0517 WHERE " +
                "username = ? AND pwd = ?";
        PreparedStatement statement =
                JDBCUtil.getPreStatement(checkLogin);
        try {
            // 设置值
            statement.setString(1, username);
            statement.setString(2, pwd);

            ResultSet rs = statement.executeQuery();
            User user = null;
            if (rs.next()) {
                user = new User();
                String dbUsername = rs
                        .getString("username");
                String dbpwd = rs
                        .getString("pwd");
                user.setUsername(dbUsername);
                user.setPwd(dbpwd);
            }
            rs.close();
            JDBCUtil.closeResource(statement);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
